package controller;

import dao.IUserDao;
import entity.Users;
import dao.impl.UserDaoImpl;
import service.IUserService;
import service.impl.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session=request.getSession();
        String name=request.getParameter("username");
        String pwd=request.getParameter("password");
        IUserService iud=new UserService();
        Users u=iud.login(name,pwd);
        Users uid=iud.getUid(name);
        if (u!=null){
            session.setAttribute("user",uid);
            request.getRequestDispatcher("pag").forward(request,response);
        }else {
            response.sendRedirect("/login.html");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       doGet(request,response);
    }
}
